package com.lz.sports.bean

import android.os.Parcel
import android.os.Parcelable
import android.text.TextUtils
import androidx.room.*
import com.google.gson.annotations.Expose
import com.lz.sports.utils.DateUtils
import com.lz.sports.utils.UploadUtils
import java.io.File

//考生
@Entity(tableName = "Examinee", indices = [Index("idCard"), Index("examineesNo")])
class Examinee() : Parcelable {

    //学生ID
    @PrimaryKey
    @ColumnInfo(name = "id")
    var id: Int = 0

    //姓名
    @Expose
    @ColumnInfo(name = "studentName")
    var studentName: String = ""

    //性别
    @Expose
    @ColumnInfo(name = "sex")
    var sex: Int = 1

    //身份证号
    @Expose
    @ColumnInfo(name = "idCard")
    var idCard: String = ""

    //准考证号
    @Expose
    @ColumnInfo(name = "examineesNo")
    var examineesNo: String = ""

    //照片地址
    @Expose
    @ColumnInfo(name = "photo")
    var photo: String = ""

    //学校ID
    @Expose
    @ColumnInfo(name = "schoolId")
    var schoolId: Int = 0

    //学校
    @Expose
    @ColumnInfo(name = "schoolName")
    var schoolName: String = ""

    //年级
    @Expose
    @ColumnInfo(name = "schoolAge")
    var schoolAge: String = ""

    //班级
    @Expose
    @ColumnInfo(name = "schoolClass")
    var schoolClass: String = ""

    //所属计划
    @Expose
    @ColumnInfo(name = "examPlanId")
    var examPlanId: Int = 0

    //所属计划编号
    @Expose
    @ColumnInfo(name = "examPlanCode")
    var examPlanCode: String = ""

    //所属考区
    @Expose
    @ColumnInfo(name = "examAreaId")
    var examAreaId: Int = 0

    //所属考区编号
    @Expose
    @ColumnInfo(name = "examAreaCode")
    var examAreaCode: String = ""

    //考点
    @Expose
    @ColumnInfo(name = "examSiteId")
    var examSiteId: Int = 0

    //考点编号
    @Expose
    @ColumnInfo(name = "examSiteCode")
    var examSiteCode: String = ""

    //报考的项目
    @Expose
    @Ignore
    var examProjectList: IntArray? = null

    //考试项目成绩表, 一个项目对应一个成绩
    @Ignore
    var examResults: List<ExamResult>? = null

    //签到信息
    @Ignore
    var examSignIn: ExamSignIn? = null

    //签到时间
    fun signInTime(): String {
        if (examSignIn != null) {
            val time = DateUtils.format(examSignIn!!.signInTime)
            if (!TextUtils.isEmpty(time)) return time
        }
        return ""
    }

    fun isMale(): Boolean = sex == 1

    //项目成绩和检录
    fun resultWithInspect(): List<ExamResultWithInspect> {
        if (examResults == null || examResults!!.isEmpty()) {
            return emptyList()
        }
        val list = ArrayList<ExamResultWithInspect>()
        examResults!!.forEach {
            if (it.inspectList == null || it.inspectList!!.isEmpty()) {
                list.add(ExamResultWithInspect(it, null))
            } else {
                it.inspectList!!.forEach { inspect ->
                    list.add(ExamResultWithInspect(it, inspect))
                }
            }
        }
        return list
    }

    //报名照
    fun examPhotoFile(): File = File(UploadUtils.examineePhotoPath(), "$examineesNo.jpg")

    @Ignore
    constructor(
        studentName: String,
        sex: Int,
        idCard: String,
        examineesNo: String,
        photo: String,
        schoolName: String,
        schoolAge: String,
        schoolClass: String,
        examPlanId: Int,
        examPlanCode: String,
        examAreaId: Int,
        examAreaCode: String,
        examSiteId: Int,
        examSiteCode: String,
        examProjectId: Int
    ) : this() {
        this.studentName = studentName
        this.sex = sex
        this.idCard = idCard
        this.examineesNo = examineesNo
        this.photo = photo
        this.schoolName = schoolName
        this.schoolAge = schoolAge
        this.schoolClass = schoolClass

        this.examPlanId = examPlanId
        this.examPlanCode = examPlanCode
        this.examAreaId = examAreaId
        this.examAreaCode = examAreaCode
        this.examSiteId = examSiteId
        this.examSiteCode = examSiteCode
        this.examProjectList = intArrayOf(examProjectId)
    }

    @Suppress("DEPRECATION")
    @Ignore
    constructor(parcel: Parcel) : this() {
        id = parcel.readInt()
        studentName = parcel.readString()!!
        sex = parcel.readInt()
        idCard = parcel.readString()!!
        examineesNo = parcel.readString()!!
        photo = parcel.readString()!!
        schoolId = parcel.readInt()
        schoolName = parcel.readString()!!
        schoolAge = parcel.readString()!!
        schoolClass = parcel.readString()!!
        examPlanId = parcel.readInt()
        examPlanCode = parcel.readString()!!
        examAreaId = parcel.readInt()
        examAreaCode = parcel.readString()!!
        examSiteId = parcel.readInt()
        examSiteCode = parcel.readString()!!
        examProjectList = parcel.createIntArray()
        examResults = parcel.createTypedArrayList(ExamResult.CREATOR)
        examSignIn = parcel.readParcelable(ExamSignIn::class.java.classLoader)
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(id)
        parcel.writeString(studentName)
        parcel.writeInt(sex)
        parcel.writeString(idCard)
        parcel.writeString(examineesNo)
        parcel.writeString(photo)
        parcel.writeInt(schoolId)
        parcel.writeString(schoolName)
        parcel.writeString(schoolAge)
        parcel.writeString(schoolClass)
        parcel.writeInt(examPlanId)
        parcel.writeString(examPlanCode)
        parcel.writeInt(examAreaId)
        parcel.writeString(examAreaCode)
        parcel.writeInt(examSiteId)
        parcel.writeString(examSiteCode)
        parcel.writeIntArray(examProjectList)
        parcel.writeTypedList(examResults)
        parcel.writeParcelable(examSignIn, flags)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Examinee> {
        override fun createFromParcel(parcel: Parcel): Examinee {
            return Examinee(parcel)
        }

        override fun newArray(size: Int): Array<Examinee?> {
            return arrayOfNulls(size)
        }
    }

    override fun equals(other: Any?): Boolean {
        if (other !is Examinee) return false
        return other === this ||
                id == other.id && examPlanId == other.examPlanId && examAreaId == other.examAreaId && examSiteId == other.examSiteId
    }

    override fun hashCode(): Int {
        var result = 17
        result = 31 * result + id
        result = 31 * result + examPlanId
        result = 31 * result + examAreaId
        result = 31 * result + examSiteId
        result = 31 * result + examineesNo.hashCode()
        result = 31 * result + idCard.hashCode()
        return result
    }

    override fun toString(): String {
        return "Examinee(id=$id, studentName='$studentName', sex=$sex, idCard='$idCard', examineesNo='$examineesNo', " +
                "photo='$photo', schoolId=$schoolId, schoolName='$schoolName', schoolAge='$schoolAge', " +
                "schoolClass='$schoolClass', examPlanId=$examPlanId, examPlanCode='$examPlanCode'," +
                " examAreaId=$examAreaId, examAreaCode='$examAreaCode', examSiteId=$examSiteId, " +
                "examSiteCode='$examSiteCode', examProjectList=$examProjectList)"
    }
}